// pages/site-guide/institution-detail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    institution: null,
    showMap: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // 从页面参数中获取机构信息
    if (options.institution) {
      const institution = JSON.parse(decodeURIComponent(options.institution));
      this.setData({
        institution: institution
      });
    }
  },

  /**
   * 拨打电话
   */
  onCallPhone() {
    const phone = this.data.institution.phone.replace('电话:', '');
    wx.makePhoneCall({
      phoneNumber: phone,
      success: () => {
        console.log('拨打电话成功');
      },
      fail: () => {
        wx.showToast({
          title: '拨打电话失败',
          icon: 'none'
        });
      }
    });
  },

  /**
   * 查看地图
   */
  onViewMap() {
    this.setData({
      showMap: true
    });
  },

  /**
   * 关闭地图
   */
  onCloseMap() {
    this.setData({
      showMap: false
    });
  },

  /**
   * 导航到机构
   */
  onNavigate() {
    const address = this.data.institution.address.replace('地址:', '');
    wx.openLocation({
      latitude: 39.9042, // 默认北京坐标，实际应该根据地址解析
      longitude: 116.4074,
      name: this.data.institution.name,
      address: address,
      scale: 18
    });
  },

  /**
   * 分享机构信息
   */
  onShare() {
    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline']
    });
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    const institution = this.data.institution;
    return {
      title: `${institution.name} - 遗体捐献机构`,
      path: `/pages/site-guide/institution-detail?institution=${encodeURIComponent(JSON.stringify(institution))}`,
      imageUrl: '/images/gongyi.png'
    };
  },

  /**
   * 用户点击右上角分享到朋友圈
   */
  onShareTimeline() {
    const institution = this.data.institution;
    return {
      title: `${institution.name} - 遗体捐献机构`,
      imageUrl: '/images/gongyi.png'
    };
  }
})
